//
//  StatusListViewModel.swift
//  mySina
//
//  Created by mac on 15/12/22.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit
import SVProgressHUD
class StatusListViewModel: NSObject {
    class func loadHomePageData(since_id: Int64, max_id: Int64,finish:(statuses:[Status]?) -> ()) {
        //实现网络请求，get请求
        let urlString = "2/statuses/home_timeline.json"
        //判断token是否为空
        guard let token = UserAccountViewModel().token else {
            SVProgressHUD.showInfoWithStatus("请重新登陆")
            return
        }
        //网络请求的参数
        var parameters = ["access_token":token]
        if since_id > 0 {
            parameters["since_id"] = "\(since_id)"
        }
        if max_id > 0 {
            parameters["max_id"] = "\(max_id - 1)"
        }
        
        NetwookTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parameters) { (dict, error) -> () in
            if error != nil {
                //执行失败的回调
                SVProgressHUD.showInfoWithStatus("网络出错，请稍后....")
                finish(statuses: nil)
                return
            }
            //请求成功
            if let array = dict!["statuses"] as? [[String:AnyObject]] {
                //遍历数组中所有的字典，做字典转模型的操作
                var list = [Status]()
                for item in array {
                    let s = Status(dict: item)
                    list.append(s)
                }
                //执行成功的回调
                finish(statuses: list)
            }
        }
    }
}